import React from 'react';
import { Form, Input, Button, Typography, Space, message } from 'antd';
import { Link } from 'umi';
import './Login.less';

const { Title } = Typography;

interface LoginProps {}

interface LoginState {
  loading: boolean;
}

class Login extends React.Component<LoginProps, LoginState> {
  constructor(props: LoginProps) {
    super(props);
    this.state = {
      loading: false,
    };
  }

  handleSubmit = async (values: any) => {
    this.setState({ loading: true });
    try {
      // 模拟登录请求
      setTimeout(() => {
        message.success('登录成功');
        this.props.history.push('/');
      }, 1000);
    } catch (error) {
      message.error('登录失败，请检查用户名和密码');
    } finally {
      this.setState({ loading: false });
    }
  };

  render() {
    return (
      <div className="login-container">
        <div className="login-card">
          <Title level={2} className="login-title">用户登录</Title>
          <Form
            name="loginForm"
            layout="vertical"
            onFinish={this.handleSubmit}
          >
            <Form.Item
              name="username"
              label="用户名"
              rules={[{ required: true, message: '请输入用户名' }]}
            >
              <Input placeholder="请输入用户名" />
            </Form.Item>

            <Form.Item
              name="password"
              label="密码"
              rules={[{ required: true, message: '请输入密码' }]}
            >
              <Input.Password placeholder="请输入密码" />
            </Form.Item>

            <Form.Item>
              <Button
                type="primary"
                htmlType="submit"
                loading={this.state.loading}
                block
                size="large"
              >
                登录
              </Button>
            </Form.Item>
          </Form>

          <Space className="login-register-link">
            <span>还没有账号？</span>
            <Link to="/register">前往注册</Link>
          </Space>
        </div>
      </div>
    );
  }
}

export default Login;